<?php

namespace XWAM\Controller;

/**
 * DataAnalysisController 类
 *
 * @since 1.0 <2016-6-1> SoChishun <14507247@qq.com> Added.
 */
class DataAnalysisController extends AppbaseController {

    // 2016-6-2
    function data_analysis_list2($store_id = 0) {
        if (!$store_id) {
            exit('参数无效');
        }
        $m_store = new \XWAM\Model\StoreConfModel();
        $store_data = $m_store->find($store_id); // 分店信息
        $this->assign('store_data', $store_data); // 分店信息
        unset($m_store);
        $this->display();
    }

    function data_analysis_list_search() {
        $search = $_GET;
        if (empty($search['date_start'])) {
            $search['date_start'] = date('Y-m-01');
        }
        $where['time_date'][] = array('EGT', $search['date_start']);
        if (empty($search['date_end'])) {
            $search['date_end'] = date('Y-m-d');
        }
        $where['time_date'][] = array('ELT', $search['date_end']);
        if (empty($search['group'])) {
            $search['group'] = 'travel_agency';
        }
        $where['store_id'] = $search['store_id'];
        return array('where' => $where, 'search' => $search);
    }

    /**
     * 
     * @param type $store_id
     * @param type $group
     * @since 1.0 2016-6-2 SoChishun Added.
     */
    function data_analysis_list($store_id = 0, $group = 'travel_agency') {
        if (!$store_id || !$group) {
            exit('参数有误!');
        }
        $conf_id = $group . '_id';
        $conf_val = I($conf_id);
        $asearch = $this->data_analysis_list_search();
        // 获取配置项目列表
        switch ($group) {
            case 'travel_agency':
                $m_conf = new \XWAM\Model\TravelAgencyConfModel();
                $list = $m_conf->get_idtext_list($store_id);
                break;
            case 'interpretation_room':
            case 'interpreter':
            case 'guide':
                $m_conf = new \XWAM\Model\CustomConfModel();
                $list = $m_conf->get_idtext_list($store_id, $group);
                break;
        }
        // 过滤
        if ($conf_val) {
            $alist = array();
            foreach ($list as $row) {
                if ($conf_val == $row['id']) {
                    $alist[] = $row;
                    break;
                }
            }
            $list = $alist;
        }
        $store_data = $m_conf->table('t_porg_store_conf')->find($store_id); // 分店信息
        // 分析表界面设置
        $ascript = array();
        $sumdata = array();
        $awhere = $asearch['where'];
        foreach ($list as $row) {
            $id = $row['id'];
            $text = $row['text'];
            // 分析表统计数据
            $awhere[$conf_id] = $row['id'];
            $sumdata = $m_conf->table('t_plvs_order')->where($awhere)->field('ceil(sum(sichou_amount) / sum(person_count)) as sichou_amount, ceil(sum(sijin_amount) / sum(person_count)) as sijin_amount, ceil(sum(baihuo_amount) / sum(person_count)) as baihuo_amount, ceil(sum(product_amount) / sum(person_count)) as product_amount')->find();
            if (is_null($sumdata['sichou_amount'])) {
                $sumdata = array('sichou_amount' => 0, 'sijin_amount' => 0, 'baihuo_amount' => 0, 'product_amount' => 0); // 旅行社没有业绩
            }
            $js_atitle = sprintf("['%s (%d)','%s (%d)','%s (%d)']", $store_data['sale_title1'], $sumdata['sichou_amount'], $store_data['sale_title2'], $sumdata['sijin_amount'], $store_data['sale_title3'], $sumdata['baihuo_amount']);
            //$ascript[] = "<script>make_chart('$text-人均消费(元/人)', 'graph$id', [['A', 40], ['B', 30], ['C', 20], ['D', 10]], ['#99CDFB', '#3366FB', '#F89900', '#F76600'], $js_atitle);</script>";
            $ascript[] = "<script>make_chart('$text-人均消费(" . $sumdata['product_amount'] . "元/人)', 'graph$id', [['A', " . $sumdata['sichou_amount'] . "], ['B', " . $sumdata['sijin_amount'] . "], ['C', " . $sumdata['baihuo_amount'] . "]], ['#99CDFB', '#3366FB', '#F89900'], $js_atitle);</script>";
        }

        // 分组配置项
        $groups = array(
            array('id' => 'travel_agency', 'text' => '旅行社', 'url' => U('TravelAgencyConf/get_json_list', 'store_id=' . $store_id)),
            // array('id' => 'interpretation_room', 'text' => '讲解厅', 'url' => U('InterpretationRoomConf/get_json_list', 'store_id=' . $store_id)),
            array('id' => 'interpreter', 'text' => '讲解员', 'url' => U('InterpreterConf/get_json_list', 'store_id=' . $store_id)),
            array('id' => 'guide', 'text' => '导游员', 'url' => U('GuideConf/get_json_list', 'store_id=' . $store_id))
        );
        $combobox_url = '';
        $title = '';
        foreach ($groups as $row) {
            if ($group == $row['id']) {
                $combobox_url = $row['url'];
                $title = $row['text'];
                break;
            }
        }
        $combobox = '<select id="group_id" name="' . $conf_id . '" data-url="' . $combobox_url . '" data-value="' . I($conf_id) . '"><option>请选择</option></select>';
        // 模板变量
        $this->assign('combobox', $combobox);
        $this->assign('title', $title);
        $this->assign('groups', $groups);
        $this->assign('scripts', implode(PHP_EOL, $ascript));
        $this->assign('list', $list);
        $this->assign('search', $asearch['search']);
        $this->assign('store_data', $store_data); // 分店信息
        $this->display();
    }

    function tourist_origin_analysis_list($store_id = 0, $tourist_origin = '') {
        if (!$store_id) {
            exit('参数有误!');
        }
        $asearch = $this->data_analysis_list_search();
        if ($tourist_origin) {
            $asearch['where']['tourist_origin'] = $tourist_origin;
        }
        $m_order = new \XWAM\Model\OrderModel();
        $list = $m_order->where($asearch['where'])->field('tourist_origin, ceil(sum(sichou_amount) / sum(person_count)) as sichou_amount, ceil(sum(sijin_amount) / sum(person_count)) as sijin_amount, ceil(sum(baihuo_amount) / sum(person_count)) as baihuo_amount, ceil(sum(product_amount) / sum(person_count)) as product_amount')->group('tourist_origin')->select();
        $store_data = $m_order->table('t_porg_store_conf')->find($store_id); // 分店信息

        $aitems = array();
        $asort = array();
        foreach ($list as $row) {
            $asort[] = $row['product_amount'];
            $aitems[$val] = $val;
        }
        array_multisort($asort, SORT_DESC, $list); // 排序
        // 分组配置项
        $groups = array(
            array('id' => 'travel_agency', 'text' => '旅行社', 'url' => U('TravelAgencyConf/get_json_list', 'store_id=' . $store_id)),
            // array('id' => 'interpretation_room', 'text' => '讲解厅', 'url' => U('InterpretationRoomConf/get_json_list', 'store_id=' . $store_id)),
            array('id' => 'interpreter', 'text' => '讲解员', 'url' => U('InterpreterConf/get_json_list', 'store_id=' . $store_id)),
            array('id' => 'guide', 'text' => '导游员', 'url' => U('GuideConf/get_json_list', 'store_id=' . $store_id))
        );
        $combobox = '<select name="tourist_origin"><option>请选择</option>' . sofn_combobox_option($aitems, $tourist_origin, '') . '</select>';
        $this->assign('combobox', $combobox);
        $this->assign('groups', $groups);
        $this->assign('list', $list);
        $this->assign('search', $asearch['search']);
        $this->assign('store_data', $store_data); // 分店信息
        $this->display();
    }

}
